package com.edar.web.platform;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.edar.components.AccountBean;
import com.edar.dao.util.Page;
import com.edar.model.Account;
import com.edar.service.platform.AccountService;
import com.edar.web.struts2.GenericAction;
@Controller
@Scope("prototype")
@Results( @Result(name = "list", type = "redirect", location = "account.action"))
public class AccountAction extends GenericAction<AccountBean> {
	private static final long serialVersionUID = 1900042912756344244L;
	@Autowired
	@Qualifier("accountServiceImpl")
	private AccountService accountService;
	private AccountBean account;
	private Page<AccountBean> page = new Page<AccountBean>(10,true);
	public AccountBean getAccount() {
		return account;
	}

	public void setAccountBean(final AccountBean account) {
		this.account = account;
	}

	public Page<AccountBean> getPage() {
		return page;
	}

	public void setPage(final Page<AccountBean> page) {
		this.page = page;
	}
	@Override
	@Action(value="/platform/account-delete",
			results={@Result(name="success",type="redirectAction",params={"actionName","account-list"})}
			)
	public String delete(){
		accountService.delete(key);
		return SUCCESS;
	}

	@Override
	@Action(value="/platform/account-list")
	public String list(){
		 accountService.list(page,account);
		return SUCCESS;
	}
	public void prepareList(){
		prepareModel();
	}
	public void prepare(){
		if(account==null){
			account = new AccountBean();
		}
	}
	@Action(value="/platform/account-input")
	public String input(){
		prepareModel();
		return SUCCESS;
	}
	@Override
	protected void prepareModel(){
		if(account.getAccountId()!=null){
			account = (AccountBean)dozer.map(accountService.getAccount(account.getAccountId()),AccountBean.class);
		}
		
	}

	@Override
	@Action(value="/platform/account-save",
			results={@Result(name="success",type="redirectAction",params={"actionName","account-list"})}
		)
	public String save(){
		Account a = (Account) dozer.map(account, Account.class);
		accountService.save(a);
		account.setAccountId(account.getAccountId());
		return SUCCESS;
	}

	public AccountBean getModel() {
		return account;
	}

}
